package com.zzzs.threaddemo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author zhu_shuai
 * @since 2021/6/23
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        List<FutureTask> futureTaskList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            MyCallable myCallable = new MyCallable(i);
            FutureTask<Integer> task = new FutureTask<>(myCallable);
            new Thread(task).start();
            futureTaskList.add(task);
        }
        for (FutureTask futureTask : futureTaskList) {
            System.out.println(futureTask.get());
        }
    }
}

class MyCallable implements Callable {

    private int i;

    public MyCallable(int i) {
        this.i = i;
    }

    @Override
    public Object call() throws Exception {
        System.out.println("id: " + i);
        return i;
    }
}